<template>
	<view class="work-wrap">
		<!-- 页面头部 -->
		<work-status :backgroundImage="backgroundImage" @clickLeft="clickLeft" @clickRight="clickRight"></work-status>
		
		<!-- 页面安全评估部分 -->
		<work-assess :assess="assess" :exceptionAll="exceptionAll" :shopNum="shopNum"></work-assess>
		
		<!-- 页面内容列表 -->
		<work-list :list="departmentList"></work-list>
	</view>
</template>

<script>
	import WorkStatus from '../../../components/TabBar/Work/WorkStatus.vue'
	import WorkAssess from '../../../components/TabBar/Work/WorkAssess.vue'
	import WorkList from '../../../components/TabBar/Work/WorkList.vue'
	import uniDrawer from '../../../components/uni-drawer/uni-drawer.vue'
	import uniList from '../../../components/uni-list/uni-list.vue'
	import uniListItem from '../../../components/uni-list-item/uni-list-item.vue'
	import {mapGetters, mapMutations} from 'vuex'
	export default {
		components: {WorkStatus, WorkAssess, WorkList, uniDrawer, uniList, uniListItem},
		data() {
			return {
				title: '',
				orgId: '',
				departmentList: [],
				exceptionAll: {
					total: 0,
					handleCount: 0,
					noHandleCount: 0
				},
				backgroundImageList: [
					{headName: '优秀', backgroundImage: 'linear-gradient(to right, #1ceeaf, #12d8ba)'},
					{headName: '良好', backgroundImage: 'linear-gradient(to right, #f7b700, #fda100)'},
					{headName: '一般', backgroundImage: 'linear-gradient(to right, #ff4d69, #ff3539)'}
				],
				assess: {},
				backgroundImage: '',
				shopNum: 0
			}
		},
		onLoad(option) {
			this.orgId = this.$store.getters.orgId
			this.getTodayExceptionList()
			this.getAllException()
			this.getShopNum()
		},
		onPullDownRefresh() {
			this.orgId = this.$store.getters.orgId
			this.getTodayExceptionList()
			this.getAllException()
			this.getShopNum()
			uni.stopPullDownRefresh()
		},
		methods: {
			...mapMutations(['setHasLogin', 'setUserInfo']),
			// logoutFunc() {
			// 	console.log('退出了')
			// 	this.$refs.drawer.close()
			// 	this.setHasLogin(false)
			// 	this.setUserInfo({})
			// 	uni.reLaunch({
			// 		url: '../../../pages/login/login'
			// 	})
			// },
			/**
			 * 状态栏左侧被点击
			 */
			clickLeft() {
				console.log('状态栏，左侧被点击')
				this.$refs.drawer.open()
			},
			/**
			 * 状态栏右侧被点击
			 */
			clickRight() {
				console.log('状态栏，右测被点击了')
				uni.showToast({
					icon: 'none',
					 title: '功能开发中!'
				})
			},
			/**
			 * 获取今天所有食堂异常总数列表
			 * 
			 */
			getTodayExceptionList() {
				let orgId = this.orgId
				this.$request.getTodayExceptionCountList({orgId}).then(res => {
				  console.log('食堂异常数列表', res)
				  this.departmentList = res || []
				  let allLightCoutn = 0
				  this.departmentList.forEach((item, index) => {
					this.getTodayCheckCount(item.departmentId, index)
					allLightCoutn += item.lightCount
					if (item.lightCount === 0) {
					  let {headName, backgroundImage} = this.backgroundImageList[0]
					  this.backgroundImage = backgroundImage
					  this.headName = headName
					  this.assess = {headName, backgroundImage}
					  this.$set(this.departmentList[index], 'headName', headName)
					  this.$set(this.departmentList[index], 'backgroundImage', backgroundImage)
					} else if (item.lightCount > 0 && item.lightCount < 7) {
					  let {headName, backgroundImage} = this.backgroundImageList[1]
					  this.backgroundImage = backgroundImage
					  this.headName = headName
					  this.assess = {headName, backgroundImage}
					  this.$set(this.departmentList[index], 'headName', headName)
					  this.$set(this.departmentList[index], 'backgroundImage', backgroundImage)
					} else {
					  let {headName, backgroundImage} = this.backgroundImageList[2]
					  this.backgroundImage = backgroundImage
					  this.headName = headName
					  this.assess = {headName, backgroundImage}
					  this.$set(this.departmentList[index], 'headName', headName)
					  this.$set(this.departmentList[index], 'backgroundImage', backgroundImage)
					}
				  })
				  if (allLightCoutn === 0) {
					let {headName, backgroundImage} = this.backgroundImageList[0]
					this.backgroundImage = backgroundImage
					this.headName = headName
					this.assess = {headName, backgroundImage}
				  } else if (allLightCoutn > 0 && allLightCoutn < 7) {
					let {headName, backgroundImage} = this.backgroundImageList[1]
					this.backgroundImage = backgroundImage
					this.headName = headName
					this.assess = {headName, backgroundImage}
				  } else {
					let {headName, backgroundImage} = this.backgroundImageList[2]
					this.backgroundImage = backgroundImage
					this.headName = headName
					this.assess = {headName, backgroundImage}
				  }
				}).catch(e => {
				  console.error('食堂异常数列', e)
				  this.departmentList = []
				  let {headName, backgroundImage} = this.backgroundImageList[0]
				  this.backgroundImage = backgroundImage
				  this.headName = headName
				  this.assess = {headName, backgroundImage}
				  this.$set(this.departmentList[0], 'headName', headName)
				  this.$set(this.departmentList[0], 'backgroundImage', backgroundImage)
				})
			},
			/**
			 * @Description: 获取所有异常数据
			 */
			getAllException() {
				let orgId = this.orgId
				this.$request.getTodayExceptionCount({orgId}).then(res => {
				  this.exceptionAll = res
				  console.log('获取所有异常数据', res)
				}).catch(e => {
				  console.error('获取所有异常数据', e)
				  this.exceptionAll = {
					total: '--',
					handleCount: '--',
					noHandleCount: '--'
				}
				})
			},
			/**
			 * @description 获取周边商家数目
			 * @param {Object} id
			 * @param {Object} index
			 */
			getShopNum() {
				let orgId = this.orgId
				this.$request.getShopCount({orgId}).then(res => {
				  console.log('周边商家数目', res)
				  res.forEach(i => {
					let num = parseInt(i.num)
					this.shopNum += num
				  })
				}).catch(e => {
				  console.error('周边商家数目', e)
				})
			 },
			/**
			 * 获取各个食堂每日检查记录
			 */
			getTodayCheckCount(id, index) {
				let departmentId = id
				this.$request.getTodayCheckCount({departmentId}).then(res => {
				  this.$set(this.departmentList[index], 'checkAllNum', res.totalNum)
				  this.$set(this.departmentList[index], 'alreadyNum', res.alreadyNum)
				}).catch(e => {
				  console.error('个食堂检查记录', e)
				})
			},
			toAboutPage() {
				uni.navigateTo({
					url: '../../about/about'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.work-wrap {
		.work-drawer-content {
			height: 100%;
			position: relative;
			margin: 100rpx 0rpx;
			.user-avatar {
				width: 200rpx;
				height: 200rpx;
				margin: 0 auto;
				.avatar {
					width: 200rpx;
					height: 200rpx;
				}
			}
			.user-name {
				text-align: center;
				font-size: 36rpx;
				font-weight: bold;
			}
			.work-drawer-list {
				margin: 30rpx 0rpx;
			}
			.logout-wrap {
				position: absolute;
				bottom: 200rpx;
				left: 50%;
				transform: translateX(-50%);
			}
		}
	}
	
</style>
